import axios from 'axios'
import { getToken ,removeToken } from './token'
import { history } from './history'
const http = axios.create({
  baseURL: 'http://geek.itheima.net/v1_0',
  timeout: 5000,
})

http.interceptors.request.use(config => {
  console.log(config)
  if (config.url !== '/authorizations') {
    config.headers.Authorization = `Bearer ${getToken()}`
  }

  return config
})
http.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response.status === 401) {
        removeToken ()
        history.push("/login")
    }
    return error
  }
)
export { http }
